/**
 * 
 */
package lt.ruta;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

/**
 * @author Vincentas
 *
 */
public class Utils {
	
	public static final String NEW_LINE = "\r\n";
	
	public static String readLine(InputStreamReader isr) throws IOException {
		StringBuffer stringBuffer = new StringBuffer();
		
		int character;
		while ((character = isr.read()) != -1 && character != '\n') {
			stringBuffer.append((char) character);
		}
		
		if (stringBuffer.length() != 0 && stringBuffer.charAt(stringBuffer.length() - 1) == '\r') {
			stringBuffer.setLength(stringBuffer.length() - 1);
		}
		
		return character == -1 && stringBuffer.length() == 0 ? null : stringBuffer.toString();
	}
	
	public static Hashtable readProperties(String resource) {
		Hashtable result = new Hashtable();
		
		InputStream inputStream = Utils.class.getResourceAsStream(resource);
		if (inputStream != null) {
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			
			String line;
			try {
				while ((line = readLine(inputStreamReader)) != null) {
					int indexOf = line.indexOf('=');
					result.put(line.substring(0, indexOf), line.substring(indexOf + 1));
				}
			} catch (IOException exception) {
				exception.printStackTrace();
			}
		}
		
		return result;		
	}
	
	public static String[] split(String text, char separator) {
		Vector vector = new Vector();
		
		int indexOf;
		while ((indexOf = text.indexOf(separator)) != -1) {
			vector.addElement(text.substring(0, indexOf));
			text = text.substring(indexOf + 1);
		}
		
		vector.addElement(text);
		
		String[] result = new String[vector.size()];
		vector.copyInto(result);
		
		return result;
	}
	
	private static final char QUOTE = '"';
	
	private static final char COMMA = ',';
			
	
	public static String csvJoin(String[] text) {
		StringBuffer stringBuffer = new StringBuffer();
		
		if (text.length > 0) {
			stringBuffer.append(QUOTE + text[0] + QUOTE);
			for (int i = 1; i < text.length; i++) {
				stringBuffer.append(COMMA);
				stringBuffer.append(QUOTE + text[i] + QUOTE);			
			}
		}
		
		return stringBuffer.toString();
	}

	private static boolean isEscaped(String text) {
		return text.charAt(0) == QUOTE && text.charAt(text.length() - 1) == QUOTE;
	}
	
	private static String stripQuote(String text) {
		return text.substring(1, text.length() - 1);
	}
	
	public static String[] csvSplit(String text) {
		Vector values = new Vector();		
		int indexOf;
		
		while ((indexOf = text.indexOf(COMMA)) != -1) {
			String value = text.substring(0, indexOf);
			if (isEscaped(value)) {
				values.addElement(stripQuote(value));
				text = text.substring(indexOf + 1);
			}
		}
		
		if (isEscaped(text)) {
			values.addElement(stripQuote(text));
		}
		
		String[]  result = new String[values.size()];
		values.copyInto(result);
		
		return result;
	}
	
	public static String formatDate(long timestamp) {
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(new Date(timestamp));
		
		return 
			"" + calendar.get(Calendar.YEAR) + 
			"." + trail((calendar.get(Calendar.MONTH) + 1), '0', 2) + 
			"." + trail(calendar.get(Calendar.DAY_OF_MONTH), '0', 2) + 
			" " + trail(calendar.get(Calendar.HOUR_OF_DAY), '0', 2) + 
			":" + trail(calendar.get(Calendar.MINUTE), '0', 2) + 
			":" + trail(calendar.get(Calendar.SECOND), '0', 2);
	}
	
	private static String trail(int value, char c, int length) {
		return trail(String.valueOf(value), c, length);
	}
	
	public static String trail(String text, char c, int length) {
		StringBuffer buffer = new StringBuffer(text);
		
		while (buffer.length() < length) {
			buffer.insert(0, c);
		}
		
		return buffer.toString();
	}
	


	
	
}
